<script setup lang="ts">
/* global QC */
import { ref, onMounted } from 'vue'
import { loginByQQ, bindMobile } from '@/services/user'
import { mobileRules, codeRules } from '@/utils/rules'
import { useMobileCode } from '@/composables'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
import { showSuccessToast } from 'vant'
import type { User } from '@/types/user'

const openId = ref('')
const isNeedBind = ref(false)

onMounted(() => {
    if (QC.Login.check()) {
        QC.Login.getMe((id: string) => {
            openId.value = id
            loginByQQ(id).then((res) => {
                //登录成功
                loginSuccess(res)
            }).catch(() => {
                //登录失败
                isNeedBind.value = true
            })
        })
    }
})
const mobile = ref('')
const code = ref()
const { onSend, time, form } = useMobileCode(mobile, 'bindMobile')
const store = useUserStore()
const router = useRouter()

//成功的逻辑
const loginSuccess = (res: { data: User }) => {
    store.setUser(res.data)
    router.replace(store.returnUrl || '/user')
    showSuccessToast('登录成功')
    store.setReturnUrl('')
}
//绑定
const bind = async () => {
    const res = await bindMobile({
        mobile: mobile.value,
        code: code.value,
        openId: openId.value
    })
    //执行成功逻辑
    loginSuccess(res)
}
</script>

<template>
    <div class="login-page" v-if="isNeedBind">
        <cp-nav-bar></cp-nav-bar>
        <div class="login-head">
            <h3>手机绑定</h3>
        </div>
        <van-form autocomplete="off" ref="form" @submit="bind">
            <van-field name="mobile" placeholder="请输入手机号" v-model="mobile" :rules="mobileRules"></van-field>
            <van-field name="code" placeholder="请输入验证码" v-model="code" :rules="codeRules">
                <template #button>
                    <span class="btn-send" @click="onSend" :class="{active: time> 0}">{{time > 0 ? `${time}s后再次发送` : '发送验证码'}}</span>
                </template>
            </van-field>
            <div class="cp-cell">
                <van-button style="margin-top: 50px" block round type="primary" native-type="submit">
                    立即绑定
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<style lang="scss" scoped>
@use '@/styles/login.scss' as *;
</style>